package cn.zhaiyy;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ShiroTest {

    public static final Logger logger = LoggerFactory.getLogger(ShiroTest.class);

    public static void main(String[] args) {
        // 创建一个SecurityManager，并绑定到上下文
        IniSecurityManagerFactory iniSecurityManagerFactory = new IniSecurityManagerFactory("classpath:shiro.ini");
        SecurityManager instance = iniSecurityManagerFactory.getInstance();
        SecurityUtils.setSecurityManager(instance);

        // 创建一个主题
        Subject subject = SecurityUtils.getSubject();
        // 使用用户名和密码登录
        subject.login(new UsernamePasswordToken("admin", "666"));
        // 断言当前用户已登录
        Assert.isTrue(subject.isAuthenticated(), "当前用户没有登录");
        logger.info("用户admin登录成功！");
        // 退出
        subject.logout();
    }
}
